package com.xqh.admin.commom.vipLesson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xqh.base.util.excel.ExcelReader;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.*;

/**
 * 解析 vip-lesson视频数据 代码
 */
public class VipLessonUtils {
    //////////////////////////以下是解析 SAT 数据

    public static void main(String[] args) {
        File file = new File("D:\\SAT阅读视频表格.xlsx");
        List<String[]> list = ExcelReader.getExcelData(file);

        List<SatTmp> slist = new ArrayList<>();
        for(String[] array:list){
            SatTmp s = new SatTmp();
            String one = array[0];
            if(one.length()>=4){
                s.setYear(one.substring(0,2));
                s.setMonth(one.substring(2,4));
            }
            s.setQuesIndex(getIntValue(array[1]));
            s.setAudioUrl(array[2]);
            slist.add(s);
        }

        List<SatRes> resList = new ArrayList<>();

        /**
         * 拼装  列表查询数据 结构
         */
        for(SatTmp s:slist){
            if(!judgeExists(resList,s)){
                addSatRes(resList, s);
            }else{
                //已存在该年份数据
                modifySatRes(resList, s);
            }
        }

        JSONObject yearJson = new JSONObject();
        for(SatRes res:resList){
            yearJson.put(res.getYear(),res.getList());
        }

        System.out.println(JSON.toJSONString(yearJson));


//        JSONObject json = new JSONObject();
//        for(SatTmp s:slist){
//           json.put(s.getYear()+"_"+s.getMonth()+"_"+s.getQuesIndex(), s);
//        }
//        System.out.println(JSON.toJSONString(json));
    }

    public static void modifySatRes(List<SatRes> resList, SatTmp s){
        for(SatRes res:resList){
            if(res.getYear().equals(s.getYear())){
                List<SatMonthRes> mlist = res.getList();

                boolean flag = false;
                for(SatMonthRes m:mlist){
                    if(m.getMonth().equals(s.getMonth())){
                       flag = true;
                       m.getList().add(s);
                       break;
                    }
                }
                if(!flag){//当前月份 集合 不包含该数据
                    SatMonthRes n = new SatMonthRes();
                    n.setMonth(s.getMonth());
                    List<SatTmp> tlist = new ArrayList<>();
                    tlist.add(s);
                    n.setList(tlist);
                    mlist.add(n);
                }
            }
        }
    }

    public static void addSatRes(List<SatRes> resList, SatTmp s){
        SatRes res = new SatRes();
        res.setYear(s.getYear());

        SatMonthRes m = new SatMonthRes();
        m.setMonth(s.getMonth());
        List<SatMonthRes> mlist = new ArrayList<>();
        List<SatTmp> slist = new ArrayList<>();
        slist.add(s);
        m.setList(slist);
        mlist.add(m);
        res.setList(mlist);

        resList.add(res);
    }

    public static boolean judgeExists(List<SatRes> rlist, SatTmp s){
        if(CollectionUtils.isEmpty(rlist)){
            return false;
        }
        boolean flag = false;
        for(SatRes r:rlist){
            if(r.getYear().equals(s.getYear())){
                flag=true;
                break;
            }
        }
        return flag;
    }



    @Data
    public static class SatRes{

        private String year;

        private List<SatMonthRes> list;
    }

    @Data
    public static class SatMonthRes{

        private String month;

        private List<SatTmp> list;
    }


    @Data
    public static class SatTmp{

        private String year;

        private String month;

        private int quesIndex;//题号

        private String audioUrl;
    }


    //////////////////////////以下是解析 托福 数据

    public static void main111(String[] args) {
        File file = new File("D:\\TPO阅读视频表格(1).xlsx");
        List<String[]> list = ExcelReader.getExcelData(file);
        List<Tmp> tlist = new ArrayList<>();
        for(String[] array : list){
            Tmp t = new Tmp();
            t.setOfficial(getIntValue(array[0]));
            t.setPassage(getIntValue(array[1]));
            t.setQuesIndex(getIntValue(array[2]));
            t.setQuesCategory(array[3]);
            t.setAudioScript(array[4]);
            t.setAudioScript2(array[5]);
            t.setCategoryCode(getCode(t.getQuesCategory()));
            tlist.add(t);
        }
        JSONObject json = new JSONObject();
        for(Tmp t:tlist){
            json.put(t.getOfficial()+"_"+t.getPassage()+"_"+t.getQuesIndex(), t);
        }

        System.out.println(JSON.toJSONString(json));
    }

    public static void main22(String[] args) {
        File file = new File("D:\\TPO阅读视频表格(1).xlsx");
        List<String[]> list = ExcelReader.getExcelData(file);
        List<Tmp> tlist = new ArrayList<>();
        for(String[] array : list){
            Tmp t = new Tmp();
            t.setOfficial(getIntValue(array[0]));
            t.setPassage(getIntValue(array[1]));
            t.setQuesIndex(getIntValue(array[2]));
            t.setQuesCategory(array[3]);
            t.setAudioScript(array[4]);
            t.setCategoryCode(getCode(t.getQuesCategory()));
            t.setAudioScript2(array[5]);
            tlist.add(t);
        }
        List<OfficialObj> olist = new ArrayList<>();

        List<CategoryObj> clist = new ArrayList<>();

        for(Tmp t:tlist){
            if(CollectionUtils.isEmpty(olist)){
                addOne(olist, t);
            }else{
                boolean flag = false;
                for(OfficialObj off:olist){
                    if(off.getOfficialKey() == t.getOfficial()){
                        flag = true;
                        off.getList().add(t);
                    }
                }
                if(!flag){
                    addOne(olist, t);
                }
            }

            if(CollectionUtils.isEmpty(clist)){
                addCategory(clist, t);
            }else{
                boolean flag = false;
                for(CategoryObj c: clist){
                    if(c.getCategoryCode().equals(t.getCategoryCode())){
                        flag = true;
                        c.getList().add(t);
                    }
                }
                if(!flag){
                    addCategory(clist, t);
                }
            }
        }
        Collections.sort(olist, Comparator.comparingInt(OfficialObj::getOfficialKey));
        JSONObject officialJson = new JSONObject();//
        for(OfficialObj o:olist){
            officialJson.put("official_"+o.getOfficialKey(), getPassageList(o.getList()));
        }

        JSONObject categoryJson = new JSONObject();
        for(CategoryObj c:clist){
            categoryJson.put(c.getCategoryCode(), getPassageList(c.getList()));
            categoryJson.put("categoryName", c.getCategoryName());
        }

        System.out.println(JSON.toJSONString(officialJson));

//		System.out.println(JSON.toJSONString(categoryJson));

    }

    public static void addCategory(List<CategoryObj> clist, Tmp t){
        CategoryObj obj = new CategoryObj();
        List<Tmp> categoryList = new ArrayList<>();
        obj.setCategoryCode(t.getCategoryCode());
        obj.setCategoryName(t.getQuesCategory());
        categoryList.add(t);
        obj.setList(categoryList);
        clist.add(obj);
    }

    public static void addOne(List<OfficialObj> olist, Tmp t){
        OfficialObj off = new OfficialObj();
        List<Tmp> offList = new ArrayList<>();
        offList.add(t);
        off.setList(offList);
        off.setOfficialKey(t.getOfficial());
        olist.add(off);
    }

    public static int getIntValue(String value){
        Double d = Double.valueOf(value);
        return d.intValue();
    }

    public static Map<String,String> map = getMapInstance();

    public static Map<String,String> getMapInstance(){
        Map<String,String> categoryMap = new HashMap<>();
        categoryMap.put("推断题", "infer_ques");
        categoryMap.put("细节题", "details");
        categoryMap.put("指代题", "refer");
        categoryMap.put("句子简化题", "sentence_simplify");
        categoryMap.put("配对题", "pair");
        categoryMap.put("修辞目的题", "rhetoric_objective");
        categoryMap.put("文章小结题", "prose_summary");
        categoryMap.put("词汇题", "vocabulary");
        categoryMap.put("句子插入题", "sentence_insert");
        categoryMap.put("选非题", "select_false");
        return categoryMap;
    }


    public static String getCode(String name){
        return map.get(name);
    }

    @Data
    public static class OfficialObj{

        private int officialKey;

        private List<Tmp> list;

        private List<PassageObj> plist;
    }


    @Data
    public static class PassageObj{

        private int passage;

        private List<Tmp> list;
    }

    @Data
    public static class CategoryObj{

        private String categoryName;

        private String categoryCode;

        private List<Tmp> list;

        private List<PassageObj> plist;
    }


    /**
     * 获取文章维度 数据
     * @param tmpList
     * @return
     */
    public static List<PassageObj> getPassageList(List<Tmp> tmpList){
        List<PassageObj> plist = new ArrayList<>();

        for(Tmp t:tmpList){
            if(plist.size()>0){
                boolean flag = true ;

                for(PassageObj p:plist){
                    if(p.getPassage() == t.getPassage()){
                        flag = false;
                        p.getList().add(t);
                    }
                }
                if(flag){
                    addPassage(plist, t);
                }
            }else{
                addPassage(plist, t);
            }
        }
        return plist;
    }

    public static void addPassage(List<PassageObj> plist, Tmp t){
        PassageObj obj = new PassageObj();
        List<Tmp> tlist = new ArrayList<>();
        obj.setPassage(t.getPassage());
        tlist.add(t);

        obj.setList(tlist);
        plist.add(obj);
    }

    @Data
    public static class Tmp{
        private int official; //第几套题
        private int passage; //第几篇文章
        private int quesIndex; //文章中第几个题目
        private String quesCategory; //题目类型
        private String categoryCode;//题目编码
        private String audioScript;//音频脚本
        private String audioScript2;//音频脚本2
    }
}
